package com.yfp.common.redis.utils;

import com.yfp.common.core.constant.CacheConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

/**
 * 
 * @version 1.0
 * @date 2024-08-22 9:33
 */
@Component
public class RedisNumber {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 充值编号
     * @return
     */
    public String getCRG(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 格式化日期为字符串
        String dateTime = currentDate.format(formatter);
        String key = CacheConstants.ENTERPRISE_CAPITAL_CRG + dateTime;
        if (redisTemplate.hasKey(key)) {
            // 缓存value自增，拿到自增后的值
            Long value = redisTemplate.opsForValue().increment(key);
            String paddedNumber = String.format("%05d", value);
            return ("CRG-" + dateTime + "-" + paddedNumber);
        } else {
            redisTemplate.opsForValue().set(key, 1, 1L, TimeUnit.DAYS);
            String paddedNumber = String.format("%05d", 1);
            return ("CRG-" + dateTime + "-" + paddedNumber);
        }
    }



    /**
     * 订单编号
     * @return
     */
    public String getORD(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 格式化日期为字符串
        String dateTime = currentDate.format(formatter);
        String key = CacheConstants.ENTERPRISE_CAPITAL_ORD + dateTime;
        if (redisTemplate.hasKey(key)) {
            // 缓存value自增，拿到自增后的值
            Long value = redisTemplate.opsForValue().increment(key);
            String paddedNumber = String.format("%05d", value);
            return ("ORD-" + dateTime + "-" + paddedNumber);
        } else {
            redisTemplate.opsForValue().set(key, 1, 1L, TimeUnit.DAYS);
            String paddedNumber = String.format("%05d", 1);
            return ("ORD-" + dateTime + "-" + paddedNumber);
        }
    }




    /**
     * 支出编号
     * @return
     */
    public String getEXP(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 格式化日期为字符串
        String dateTime = currentDate.format(formatter);
        String key = CacheConstants.ENTERPRISE_CAPITAL_EXP + dateTime;
        if (redisTemplate.hasKey(key)) {
            // 缓存value自增，拿到自增后的值
            Long value = redisTemplate.opsForValue().increment(key);
            String paddedNumber = String.format("%05d", value);
            return ("EXP-" + dateTime + "-" + paddedNumber);
        } else {
            redisTemplate.opsForValue().set(key, 1, 1L, TimeUnit.DAYS);
            String paddedNumber = String.format("%05d", 1);
            return ("EXP-" + dateTime + "-" + paddedNumber);
        }
    }



    /**
     * 券码编号
     * @return
     */
    public String getCPN(){
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 格式化日期为字符串
        String dateTime = currentDate.format(formatter);
        String key = CacheConstants.ENTERPRISE_CAPITAL_CPN + dateTime;
        if (redisTemplate.hasKey(key)) {
            // 缓存value自增，拿到自增后的值
            Long value = redisTemplate.opsForValue().increment(key);
            String paddedNumber = String.format("%05d", value);
            return ("CPN-" + dateTime + "-" + paddedNumber);
        } else {
            redisTemplate.opsForValue().set(key, 1, 1L, TimeUnit.DAYS);
            String paddedNumber = String.format("%05d", 1);
            return ("CPN-" + dateTime + "-" + paddedNumber);
        }
    }


}
